#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , gen(std::random_device()())
    , dis(1, 100)
{
    ui->setupUi(this);
    setWindowTitle("球员能力评估器");
    setWindowIcon(QIcon(":/MJvsLeBron.png"));
    resize(1000, 800);

    std::random_device rd;
    gen = std::mt19937(rd());
    dis = std::uniform_int_distribution<>(1, 100);

    ui->lineEdit->setPlaceholderText("输入你心目中的球星...");

    ui->AttackBar->setRange(0, 100);
    ui->ProtectBar->setRange(0, 100);
    ui->OriginalBar->setRange(0, 100);
    ui->AttackBar->setValue(0);
    ui->ProtectBar->setValue(0);
    ui->OriginalBar->setValue(0);
    ui->LCD1->display(0);
    ui->LCD2->display(0);
    ui->LCD3->display(0);

    evaluationTimer = new QTimer(this);
    connect(ui->pushButton, &QPushButton::clicked, this, &Widget::startEvaluation);
    connect(evaluationTimer, &QTimer::timeout, this, &Widget::updateEvaluation);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::startEvaluation()
{
    QString playerName = ui->lineEdit->text();
    if(playerName.isEmpty()){
        QMessageBox::warning(this, "错误", "什么都不输入，有病？");
        return;
    }

    ui->AttackBar->setValue(0);
    ui->ProtectBar->setValue(0);
    ui->OriginalBar->setValue(0);
    ui->LCD1->display(0);
    ui->LCD2->display(0);
    ui->LCD3->display(0);

    setupHallOfFamePlayers(playerName);

    currentAttack = 0;
    currentProtect = 0;
    currentOriginal = 0;

    ui->pushButton->setEnabled(false);
    ui->lineEdit->setEnabled(false);

    evaluationTime = 0;
    evaluationTimer->start(50);

}

void Widget::updateEvaluation()
{
    // 更新进攻能力
    if(currentAttack < targetAttack) {
        currentAttack += 1 + dis(gen) % 3;
        if(currentAttack > targetAttack) currentAttack = targetAttack;
        ui->AttackBar->setValue(currentAttack);
        ui->LCD1->display(currentAttack);
    }

    // 更新防守能力
    if(currentProtect < targetProtect) {
        currentProtect += 1 + dis(gen) % 2;
        if(currentProtect > targetProtect) currentProtect = targetProtect;
        ui->ProtectBar->setValue(currentProtect);
        ui->LCD2->display(currentProtect);
    }

    // 更新组织能力
    if(currentOriginal < targetOriginal) {
        currentOriginal += 1;
        if(currentOriginal > targetOriginal) currentOriginal = targetOriginal;
        ui->OriginalBar->setValue(currentOriginal);
        ui->LCD3->display(currentOriginal);
    }

    // 检查是否所有值都达到目标
    if(currentAttack >= targetAttack &&
            currentProtect >= targetProtect &&
            currentOriginal >= targetOriginal)
    {
        evaluationTimer->stop();
        ui->pushButton->setEnabled(true);
        ui->lineEdit->setEnabled(true);

        QMessageBox::information(this, "评估完成",
                                 QString("球员 %1 的最终能力值：\n进攻：%2\n防守：%3\n组织：%4")
                                 .arg(ui->lineEdit->text())
                                 .arg(targetAttack)
                                 .arg(targetProtect)
                                 .arg(targetOriginal)
                                 );
    }

}

QString Widget::getPlayerGrade(int attack, int protect, int original)
{
    int total = attack + protect + original;

    if(total >= 280) return "🏆 传奇巨星 (SSS级)";
    if(total >= 260) return "⭐ 超级巨星 (SS级)";
    if(total >= 240) return "🌟 全明星 (S级)";
    if(total >= 220) return "👍 首发球员 (A级)";
    if(total >= 190) return "👌 轮换球员 (B级)";
    if(total >= 160) return "🤔 替补球员 (C级)";
    return "😅 发展联盟 (D级)";
}

void Widget::setupHallOfFamePlayers(const QString &playerName)
{
    QString name = playerName.toLower();

    if(name.contains("jordan") || name.contains("乔丹")) {
        targetAttack = 98;
        targetProtect = 97;
        targetOriginal = 90;
        QMessageBox::information(this, "Goat", "if you want to win, you have to learn from failure");
    }
    else if(name.contains("james") || name.contains("詹姆斯") || name.contains("lebron")) {
        targetAttack = 97;
        targetProtect = 94;
        targetOriginal = 96;
    }
    else if(name.contains("duncan") || name.contains("邓肯")) {
        targetAttack = 90;
        targetProtect = 98;
        targetOriginal = 88;
    }
    else if(name.contains("curry") || name.contains("库里")) {
        targetAttack = 99;
        targetProtect = 85;
        targetOriginal = 95;
    }
    else if(name.contains("jokic") || name.contains("约基奇")) {
        targetAttack = 92;
        targetProtect = 86;
        targetOriginal = 99;
    }
    else if(name.contains("kobe") || name.contains("科比")) {
        targetAttack = 99;
        targetProtect = 94;
        targetOriginal = 89;
        // 曼巴精神彩蛋
        QMessageBox::information(this, "曼巴精神", "Man Mamba out...See you again");
    }
    else {
        // 普通球员随机生成
        targetAttack = dis(gen);
        targetProtect = dis(gen);
        targetOriginal = dis(gen);

        // 根据首字母设置下限
        int minValue = 1;
        QChar firstChar = playerName.at(0).toUpper();
        if(firstChar == 'A') minValue = 80;
        else if(firstChar == 'B') minValue = 70;
        else if(firstChar == 'C') minValue = 60;
        else if(firstChar == 'D') minValue = 40;

        if(targetAttack < minValue) targetAttack = minValue + dis(gen) % (100 - minValue + 1);
        if(targetProtect < minValue) targetProtect = minValue + dis(gen) % (100 - minValue + 1);
        if(targetOriginal < minValue) targetOriginal = minValue + dis(gen) % (100 - minValue + 1);
    }
}

